using UnityEngine;

// 自动创建并挂载单例脚本，不要手动挂载
public class SingletonMonoBase<T> : MonoBehaviour where T : MonoBehaviour
{   
    private static T instance;

    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                string name = typeof(T).Name;
                GameObject go = GameObject.Find(name);
                if (go == null)
                {
                    go = new GameObject(name);
                    instance = go.GetComponent<T>();
                    if (instance == null)
                    {
                        instance = go.AddComponent<T>();
                    }
                }
            }
            return instance;
        }
    }
}
